package com.common.tools.controller;

import com.common.api.annontation.Authority;
import com.common.api.annontation.ServiceLog;
import com.common.api.enums.OperateEnum;
import com.common.api.model.PageModel;
import com.common.tools.api.dto.ParamDto;
import com.common.tools.api.service.ParamService;
import com.common.tools.util.BaseBusyServiceUtil;
import com.common.web.annotation.ControllerRequest;
import com.common.web.controller.BaseController;
import com.common.web.dto.request.FuzzyPageDTO;
import com.common.web.dto.response.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 * 参数表前端控制器
 * </p>
 *
 * @author zhuchao
 */
@ServiceLog
@ControllerRequest("/param/")
@Api(tags = "系统工具-参数表服务接口")
public class ParamController extends BaseController {


    @Autowired
    private ParamService paramService;


    /**
     * 查询所有参数信息，分页展示
     *
     * @param fuzzyPageDTO
     * @return
     */
    @ApiOperation(value = "查询所有参数信息")
    @RequestMapping(value = "queryList")
    public RestResponse queryList(@RequestBody FuzzyPageDTO fuzzyPageDTO) {
        try {
            PageModel<ParamDto> page = paramService.queryList(fuzzyPageDTO);
            return RestResponse.success("参数查询成功!").setData(page);
        } catch (Exception e) {
            return RestResponse.failure("参数查询失败：" + e.getMessage());
        }
    }


    /**
     * 保存参数信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "save")
    @ApiOperation(value = "保存参数信息")
    @Authority(authous = {"param/paramEdit"})
    public RestResponse save(@RequestBody ParamDto dto) {
        String operation = StringUtils.isBlank(dto.getId()) ? OperateEnum.addParam.getEnumName() : OperateEnum.updateParam.getEnumName();
        try {
            executeParamOperation(dto, operation);
            return RestResponse.success(operation + "成功！");
        } catch (Exception e) {
            return RestResponse.failure(operation + "失败：" + e.getMessage());
        }
    }

    private void executeParamOperation(ParamDto dto, String operation) {
        if (OperateEnum.addParam.getEnumName().equals(operation)) {
            logOperateDetails(OperateEnum.addParam, getUserCode(), dto);
            paramService.add(dto);
        } else if (OperateEnum.updateParam.getEnumName().equals(operation)) {
            logOperateDetails(OperateEnum.updateParam, getUserCode(), dto);
            paramService.modify(dto);
        }
    }

    private void logOperateDetails(OperateEnum operateEnum, String paramCode, ParamDto dto) {
        BaseBusyServiceUtil.addOperateLog(operateEnum, paramCode, dto);
    }

    /**
     * 根据主键批量删除参数
     *
     * @param idList
     * @return
     */
    @RequestMapping(value = "dels")
    @ApiOperation(value = "根据主键批量删除参数")
    @Authority(authous = {"param/paramEdit"})
    public RestResponse dels(@RequestBody List<String> idList) {
        try {
            paramService.dels(idList);
            String logMessage = "删除参数" + idList.toString() + "成功！";
            BaseBusyServiceUtil.addOperateLog(OperateEnum.removeParam, getUserCode(), logMessage);
            return RestResponse.success("参数删除成功！");
        } catch (Exception e) {
            return RestResponse.failure("参数删除失败：" + e.getMessage());
        }
    }
}
